<?php

$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
$date=date('Y-m-d',time());//当日
$mdb=MaintenanceShops::where("status",1)->get();//所有正常门店
foreach ($mdb as $_mdb) {//计算每个店的数据
    $ctrl_old = CtrlDataEveryday::where('shop_id',$_mdb->id)->where('execute_date',$date)->update(array("status"=>2));
    //计算 监控数据
    $export_detail = Report::get_shop_control_data($date,$_mdb->id);
    //保存
    $ctrl = new CtrlDataEveryday();
    $ctrl->shop_id = $_mdb->id;
    $ctrl->execute_date = $date;
    $ctrl->order_count = $export_detail['order_count'];
    $ctrl->order_payable_amount = $export_detail['order_payable_amount'];
    $ctrl->order_fitting_amount = $export_detail['order_fitting_amount'];
    $ctrl->order_gross_profit = $export_detail['order_gross_profit'];
    $ctrl->order_gross_profit_rate = $export_detail['order_gross_profit_rate'];
    $ctrl->order_average = $export_detail['order_average'];
    $ctrl->order_new_cars = $export_detail['order_new_cars'];
    $ctrl->order_add_cars = $export_detail['order_add_cars'];
    $ctrl->order_rePurchase_rate = $export_detail['order_rePurchase_rate'];
    $ctrl->order_keep_rate = $export_detail['order_keep_rate'];
    $ctrl->order_stay_sum = $export_detail['order_stay_sum'];
    $ctrl->order_count_pur_station = $export_detail['order_count_pur_station'];
    $ctrl->order_amount_pur_station = $export_detail['order_amount_pur_station'];
    $ctrl->order_count_pur_man = $export_detail['order_count_pur_man'];
    $ctrl->order_amount_pur_man = $export_detail['order_amount_pur_man'];
    $ctrl->status = 1;
    $ctrl->data_type = 1;
    try {
        $ctrl->save();
    }catch(Exception $e){
        Log::error("error", array("system" => "api" ,"event" => "save data", 'data' =>$ctrl->toArray() ,"exception" => $e));
    }
    //统计分类
    $export_detail_1 = Report::get_shop_control_data_classify($date,$_mdb->id);
    //保存
    foreach($export_detail_1 as $ex) {
        $ctrll = new CtrlDataEveryday();
        $ctrll->shop_id = $_mdb->id;
        $ctrll->execute_date = $date;
        $ctrll->order_count = $ex['order_count'];
        $ctrll->order_payable_amount = $ex['order_payable_amount'];
        $ctrll->order_fitting_amount = $ex['order_fitting_amount'];
        $ctrll->order_gross_profit = $ex['order_gross_profit'];
        $ctrll->order_gross_profit_rate = $ex['order_gross_profit_rate'];
        $ctrll->order_average = $ex['order_average'];
        $ctrll->order_new_cars = $ex['order_new_cars'];
        $ctrll->order_add_cars = $ex['order_add_cars'];
        $ctrll->order_rePurchase_rate = $ex['order_rePurchase_rate'];
        $ctrll->order_keep_rate = $ex['order_keep_rate'];
        $ctrll->order_stay_sum = $ex['order_stay_sum'];
        $ctrll->order_count_pur_station = $ex['order_count_pur_station'];
        $ctrll->order_amount_pur_station = $ex['order_amount_pur_station'];
        $ctrll->order_count_pur_man = $ex['order_count_pur_man'];
        $ctrll->order_amount_pur_man = $ex['order_amount_pur_man'];
        $ctrll->classify_id = $ex['classify_id'];
        $ctrll->classify_name = $ex['classify_name'];
        $ctrll->status = 1;
        $ctrll->data_type = 2;
        try {
            $ctrll->save();
        } catch (Exception $e) {
            Log::error("error", array("system" => "api", "event" => "save data", 'data' => $ctrll->toArray(), "exception" => $e));
        }
    }
}
?>